package ua.org.extjsbuilder.parser.scanner;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ua.org.extjsbuilder.parser.IToken;
import ua.org.extjsbuilder.parser.ITokenList;
import ua.org.extjsbuilder.parser.runtime.JSGlobalToken;
import ua.org.extjsbuilder.parser.runtime.UnparsedToken;
import junit.framework.TestCase;

public class LinesScannerTestCase extends TestCase {
	private static final String MOCK_PKG = "ua/org/extjsbuilder/parser/mock/";
	
	public void testScan() throws Exception{
		List<IToken> result = getResultForResource("test1.js");
		assertEquals(21, result.size());
		for(int i=1;i<result.size();i++){
			assertEquals(UnparsedToken.class, result.get(i).getClass());
			assertEquals(i, result.get(i).getLine());
		}
		assertEquals(3, getResultForResource("test2.js").size());
		assertEquals(9, getResultForResource("test5.js").size());
	}
	
	private List<IToken> getResultForResource(String resource) throws IOException {
		ITokenList global = getGlobalFor(resource);
		global.scan(new LinesScanner());
		return asStdList(global);
	}
	
	private List<IToken> asStdList(ITokenList global) {
		List<IToken> result = new ArrayList<IToken>();
		IToken t = global.getHead();
		while(t!=null){
			result.add(t);
			t = t.getNext();
		}
		return result;
	}

	private ITokenList getGlobalFor(String resource) throws IOException{
		return new JSGlobalToken(new InputStreamReader(
				getStream(resource)));
	}
	
	private InputStream getStream(String resource) {
		return getClass().getClassLoader()
			.getResourceAsStream(MOCK_PKG+resource);
	}
}
